import "reflect-metadata";
import { Entity, Column, PrimaryColumn } from "typeorm";
import { createConnection, ConnectionOptions,Connection } from "typeorm";
import co = require('co');
/**
 * 数据实体（表）
 */
@Entity()
export class city {
    @PrimaryColumn("int", { length: 11 })
    id: number;
    @Column("char", { length: 35 })
    name: string;
    @Column("char", { length: 3 })
    countryCode: string;
    @Column("char", { length: 20 })
    district: string;
    @Column("int", { length: 11 })
    population: number;

    noColumn: string;
}

/**
 * 连接配置（MySQL）
 */
let conn: ConnectionOptions = {
    type: "mysql",
    host: "localhost",
    port: 3306,
    database: "world",
    username: "root",
    password: "root",
    entities: [
        city
    ],
    autoSchemaSync: false
};

function* query() {
    try {
        let cityList = yield createConnection(conn).then(connection => {
                let cityRepository = connection.getRepository(city);
                return cityRepository.findOneById(40);
            });
        return cityList;
    } catch (e) {
        console.error(e.message || `未知错误`)
    }
}

co(query()).then(function(result:city){
    console.log(result);
});


